{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5c70ed90",
   "metadata": {},
   "source": [
    "## Numpy 中的比较和Fancy Indexing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7746bd7b",
   "metadata": {},
   "source": [
    "### Fancy Indexing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "75a2cdf9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "x = np.arange(16)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "58436eb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1dc213c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[1:6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6f9cf93b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[1:6:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "98cf233a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 5, 7]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x[3], x[5], x[7]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ad34c8d",
   "metadata": {},
   "source": [
    "可以给一个序号一维数组，然后取出对应的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "56709275",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 5, 7])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ind = [3, 5, 7]\n",
    "x[ind]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ad55a5d",
   "metadata": {},
   "source": [
    "二维也是支持的，可以传一个序号的二维数组，然后去一维里面取对应序号的值并最后返回一个二维的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "dc76f706",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2],\n",
       "       [1, 3]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ind = np.array([[0, 2], [1, 3]])\n",
    "x[ind]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56423921",
   "metadata": {},
   "source": [
    "### Fancy Indexing 应用在二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9d953b0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = x.reshape(4, -1)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dd69aef4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  6, 11])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row = np.array([0, 1, 2])\n",
    "col = np.array([1, 2, 3])\n",
    "X[row, col]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ee0550c",
   "metadata": {},
   "source": [
    "上面表示取(0, 1) (1, 2) 和(2, 3) 这个三个坐标对应的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "99b2bbf8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[0, col]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14672a22",
   "metadata": {},
   "source": [
    "上面表示取第一行的1， 2， 3列数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "07a4e8c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [5, 6, 7]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:2, col]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "440c1de7",
   "metadata": {},
   "source": [
    "表示取第一、第二行的1， 2，3列数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8879620",
   "metadata": {},
   "source": [
    "也可以通过布尔数组取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "735a3541",
   "metadata": {},
   "outputs": [],
   "source": [
    "col = [True, False, True, True]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "aaade5e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 3])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[0, col]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "360ba7a2",
   "metadata": {},
   "source": [
    "上面表示取True部分列的数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcc8cffb",
   "metadata": {},
   "source": [
    "### numpy.array 的比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ab418999",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3ca451ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True, False, False, False, False, False, False,\n",
       "       False, False, False, False, False, False, False])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x < 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "728a2cbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True, False, False, False, False, False,\n",
       "       False, False, False, False, False, False, False])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x == 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "62211b12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True, False,  True,  True,  True,  True,  True,\n",
       "        True,  True,  True,  True,  True,  True,  True])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x != 3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74275a02",
   "metadata": {},
   "source": [
    "还支持复杂的表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d0d72a27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False,  True, False, False, False, False,\n",
       "       False, False, False, False, False, False, False])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 * x == 24 - 4 * x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce4a3052",
   "metadata": {},
   "source": [
    "也支持二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6275ebec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f58fff35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True],\n",
       "       [ True,  True, False, False],\n",
       "       [False, False, False, False],\n",
       "       [False, False, False, False]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X < 6"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60f2a15b",
   "metadata": {},
   "source": [
    "### 使用 numpy.array 的比较结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47f40cd7",
   "metadata": {},
   "source": [
    "统计 x 数组中小于和等于3的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b375d3ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(x <= 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4be5260e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.count_nonzero( x <= 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9540785a",
   "metadata": {},
   "source": [
    "判断x中是否有等于0的树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "92992e55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.any(x == 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba2759fd",
   "metadata": {},
   "source": [
    "判断x中是否全部大于等于0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "35656f36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all(x >= 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "fb1c8ea3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66e0b041",
   "metadata": {},
   "source": [
    "求二维数组X中有多少个偶数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "41f2c587",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum( X % 2 == 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6d74b34",
   "metadata": {},
   "source": [
    "求每一行有多少个偶数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "63d1278c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 2, 2])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(X % 2 == 0, axis = 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00679cf1",
   "metadata": {},
   "source": [
    "求每一列有多少个偶数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f2b427e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 0, 4, 0])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(X % 2 == 0, axis = 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7d2639f",
   "metadata": {},
   "source": [
    "列的方向上面(行)大于1的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "08bbf0e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False,  True,  True,  True])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all( X > 1, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5ded1b3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffc44f56",
   "metadata": {},
   "source": [
    "假如上面代表小孩的年龄，求大于3小于10岁的小孩个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c883b6c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum((x > 3) & (x < 10))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06f9f1ea",
   "metadata": {},
   "source": [
    "求年龄是偶数或者大于10岁的小孩数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "295c120b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum((x % 2 == 0) | (x > 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "87033b6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(~(x == 0))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e9d18d7",
   "metadata": {},
   "source": [
    "上面是求年龄大于0的小孩数量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcb4f282",
   "metadata": {},
   "source": [
    "### 比较结果和Fancy Indexing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78eb3145",
   "metadata": {},
   "source": [
    "查看小于5岁的小孩年龄"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "88624bbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True, False, False, False, False,\n",
       "       False, False, False, False, False, False, False])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x < 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ab5801f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[x < 5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "abd3416c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  2,  4,  6,  8, 10, 12, 14])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[x % 2 == 0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a56ed36",
   "metadata": {},
   "source": [
    "取出最后一列可以被3整除的每行数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "47dc4566",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "d5f7c787",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  7, 11, 15])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:,3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73c1299a",
   "metadata": {},
   "source": [
    "上面先取出最后一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "77b5ee16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[X[:,3] % 3 == 0, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "8a21e452",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3, 15])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[X[:,3] % 3 == 0, 3]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
